import json
import os

from apiview.views.rest_framework import View
from django.http import HttpResponse

from myapp.models import User
from django.db import models

# Create your models here.

class Roles(models.Model):

    role_name = models.CharField("角色名称",max_length=32,unique=True)
    class Meta:
        db_table = "roles"



#
class Approves(models.Model):
    name = models.CharField(max_length=64,verbose_name='姓名')
    descrip = models.TextField(verbose_name='描述')
    start_times = models.DateField(verbose_name='开始时间')
    end_tiems = models.DateField(verbose_name='结束时间')
    role = models.ForeignKey(to='Roles',on_delete=models.CASCADE)
    def __str__(self):
        return self.name

    class Meta:
        db_table = 'approves'



class FlowConf(models.Model):
    name = models.CharField(max_length=64,verbose_name='工作流名称')
    customfield = models.TextField(verbose_name='自定义字段')
    description = models.TextField(verbose_name='描述')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'flowconf'


#工单表
class WorkOrder(models.Model):
    # 工单名称
    flowconf = models.ForeignKey(to='FlowConf',on_delete=models.CASCADE)
    # 创建者
    create_user = models.ForeignKey(to=User,on_delete=models.CASCADE)
    # 创建时间
    create_ts = models.DateTimeField(auto_now_add=True)
    status_cat = (
        ('0', '审批中'),
        ('1', '被退回'),
        ('2', '完成'),
    )
    # 工单状态
    order_status = models.CharField(max_length=5, choices=status_cat, default='0')
    # 工单参数
    parameter = models.TextField(default={})
    # 审批序号
    sequence_number = models.IntegerField()

    class Meta:
        verbose_name_plural = '实例化工单'

    def __str__(self):
        return self.flowconf.name



class ApproveConf(models.Model):
    flowconf = models.ForeignKey(to=FlowConf,on_delete=models.CASCADE)
    sequence = models.IntegerField(verbose_name='审批序号')
    approve_type_choice = (
        ('1','指定人员审批'),
        ('2','角色组审批')
    )
    approve_type_id = models.CharField(choices=approve_type_choice,default='1',max_length=16)
    approve_id = models.CharField(max_length=32,verbose_name='审批ID')

    def __str__(self):
        return self.flowconf.name

    class Meta:
        db_table = 'approveconf'


import mongoengine


class Qinjia(models.Model):
    uid = models.IntegerField(verbose_name='请求人id')
    pid = models.CharField(max_length=100, verbose_name='当前审批人id', null=True)
    Approved_list = models.CharField(max_length=200, verbose_name='审批人队列')
    state = models.IntegerField(default=0, verbose_name='审批状态')
    refuse_reason = models.CharField(max_length=500, null=True, default=None, verbose_name='拒绝原因')
    voucher = models.ImageField(null=True, default=None)
    qiniu = models.CharField(max_length=300, null=True)
    youpai = models.CharField(max_length=300, null=True)
    info = models.CharField(max_length=500, verbose_name='请假单信息')


class YD(models.Model):
    pid = models.IntegerField(verbose_name='审批人')
    qid = models.IntegerField(verbose_name='请假单')


class Moban(mongoengine.Document):
    pid = mongoengine.IntField(max_length=100)
    geshi = mongoengine.StringField(max_length=500)

class Stea(models.Model):
    user_id = models.IntegerField()
    img = models.CharField(max_length=100)
    ping = models.TextField()
    state = models.IntegerField(default=0)
    class Meta:
        db_table = 'Stea'


